package com.whmnx.main.ui.model;

import android.app.Application;
import android.content.Intent;
import android.view.View;

import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.CommonRecyclerAdapter;
import com.tencent.qcloud.ugckit.UGCKitConstants;
import com.whmnx.base.bean.TCVideoInfo;
import com.whmnx.base.bean.VideoListBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.main.BR;
import com.whmnx.main.R;
import com.whmnx.main.databinding.FragmentLikeBinding;
import com.whmnx.main.ui.adapter.VedioAdapter3;
import com.whmnx.main.ui.fragment.LikeFragment;
import com.whmnx.main.ui.videorecord.TCVodPlayerActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/11/3 0003 9:53
 * version: 1.0
 */
public class LikeModel extends ToolbarViewMainModel{

    public LikeFragment mFragment;
    FragmentLikeBinding mBinding;

    public List<TCVideoInfo> mTCLiveInfoList  = new ArrayList<>();
    private VedioAdapter3 vedioAdapter;

    public LikeModel(@NonNull Application application) {
        super(application);
    }

    private int page = 1;
    private int pageSize = 10;

    public int status = 0;

    private Disposable subscribe;

    private List<VideoListBean> videoListBean = new ArrayList<>();

    public void initToolbar(LikeFragment worksFragment, FragmentLikeBinding binding) {
        this.mFragment = worksFragment;
        this.mBinding = binding;
        setTitleText("我的喜欢");

        GridLayoutManager gridLayoutManager = new GridLayoutManager(mFragment.getActivity(), 3);
        //设置RecycleView显示的方向是水平还是垂直 GridLayout.HORIZONTAL水平  GridLayout.VERTICAL默认垂直
        //配置布局，默认为vertical（垂直布局），下边这句将布局改为水平布局
        //  layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        // gridLayoutManager.setOrientation(GridLayout.VERTICAL);
        mBinding.rv.setLayoutManager(gridLayoutManager);
        mBinding.rv.setItemAnimator(new DefaultItemAnimator());
        vedioAdapter = new VedioAdapter3(mFragment.getActivity());
        mBinding.rv.setAdapter(vedioAdapter);
        initData(true);

        mBinding.refresh.setOnRefreshListener(refreshLayout -> initData(true));
        mBinding.refresh.setOnLoadMoreListener(refreshLayout ->initData(false));

        initView();
    }

    private void initView() {
        subscribe = RxBus.getDefault().toObservable(String.class)
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String object){
                        if (object.equals(EventConstants.LIVE)){  //申请退货退款
                            initData(true);
                        }
                    }
                });
        RxSubscriptions.add(subscribe);
    }

    private void initData(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("userId", SPUtils.getInstance().getString(Constants.USERID));
        params.put("type", 1);
        params.put("pageIndex", page);
        params.put("pageSize", pageSize);
        HttpClient.post(mFragment.getActivity(), Constants.MYWORKLIST, params,new CallBack<List<VideoListBean>>() {
            @Override
            public void onSuccess(List<VideoListBean> videoBeans) {
                status = 1;
                mTCLiveInfoList.clear();
                videoListBean.clear();
                videoListBean.addAll(videoBeans);
                for (int i = 0; i < videoBeans.size(); i++) {
                    TCVideoInfo tcVideoInfo = new TCVideoInfo();
                    tcVideoInfo.playurl = videoBeans.get(i).getWork_Vidoe();
                    mTCLiveInfoList.add(tcVideoInfo);
                    //  observableList.add(new WorkItemViewModel(WorksModel.this,videoBeans.get(i),i));
                }
                if (isRefresh) {
                    if (videoBeans.size() == 0){
                        mBinding.llEmpty.setVisibility(View.VISIBLE);
                        mBinding.rv.setVisibility(View.GONE);
                    }else {
                        mBinding.llEmpty.setVisibility(View.GONE);
                        mBinding.rv.setVisibility(View.VISIBLE);
                    }

                    mBinding.refresh.finishRefresh(true);
                    vedioAdapter.replaceAll(videoBeans, true);
                    //   workListTopAdapter.replaceAll(result, true);
                } else {
                    if (videoBeans.size() > 0) {
                        mBinding.refresh.finishLoadMore(true);
                        vedioAdapter.addAll(videoBeans);
                    } else {
                        mBinding.refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });


        vedioAdapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                Intent intent = new Intent(mFragment.getActivity(), TCVodPlayerActivity.class);
                intent.putExtra(UGCKitConstants.PLAY_URL, vedioAdapter.getItem(i).getWork_Vidoe());
                intent.putExtra(UGCKitConstants.PUSHER_ID, 2);
                //  intent.putExtra(UGCKitConstants.PUSHER_NAME, item.nickname == null ? item.userid : item.nickname);
                intent.putExtra(UGCKitConstants.PUSHER_AVATAR, vedioAdapter.getItem(i).getWork_Image());
                intent.putExtra(UGCKitConstants.COVER_PIC, vedioAdapter.getItem(i).getWork_Image());
                intent.putExtra(UGCKitConstants.FILE_ID, "");
                intent.putExtra(UGCKitConstants.TCLIVE_INFO_LIST, (Serializable) mTCLiveInfoList);
                intent.putExtra(UGCKitConstants.TIMESTAMP, (Serializable) videoListBean);
                intent.putExtra(UGCKitConstants.TCLIVE_INFO_POSITION, i);
                mFragment.getActivity().startActivityForResult(intent, 100);
            }
        });
    }


    //给RecyclerView添加ObservableList
    public ObservableList observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<WorkItemViewModel2> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_like);
}
